#include "StdAfx.h"
#include "ClientSocket.h"
#include "Message.h"
#include "ChatRoomServer.h"

CClientSocket::CClientSocket(void)
{
}

CClientSocket::~CClientSocket(void)
{	
	if(m_pArchiveIn != NULL)
	{
		delete m_pArchiveIn;
	}
	if(m_pArchiveOut != NULL)
	{
		delete m_pArchiveOut;
	}
	if (m_pFile != NULL)
	{
		delete m_pFile;
	}
}

CClientSocket::CClientSocket(CChatRoomServer* pDlg)
{
	m_pDlg = pDlg;
	m_nMsgCount = 0;
	m_pFile = NULL;
	m_pArchiveIn = m_pArchiveOut = NULL;
}

void CClientSocket::Initialize()
{
	m_pFile = new CSocketFile(this);
	m_pArchiveIn = new CArchive(m_pFile,CArchive::load);
	m_pArchiveOut = new CArchive(m_pFile,CArchive::store);
}

void CClientSocket::SendMessage(CMessage *pMsg)
{
	if(m_pArchiveOut != NULL)
	{		
		pMsg->Serialize(*m_pArchiveOut);
		m_pArchiveOut->Flush();
	}
}

void CClientSocket::ReceiveMessage(CMessage *pMsg)
{
	pMsg->Serialize(*m_pArchiveIn);
}

void CClientSocket::OnReceive(int nErrorCode)
{
	CSocket::OnReceive(nErrorCode);
	m_pDlg->OnReceive(this);
}

